﻿using Dtos;
using IBLL;
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class GradesBll : IGradesBll
    {
        private IGradesDal _dal;
        public GradesBll(IGradesDal dal)
        {
            _dal = dal;
        }
        public async Task<int> AddGradesAsync(string title)
        {
            return await _dal.AddAsync(new Model.Grades { Title = title});
        }

        public async Task<int> DeleteGradesAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return data != null ? await _dal.DeleteAsync(data) : 0;
        }

        public async Task<List<GradesDto>> GetAll()
        {
            var data = await _dal.GetAll();
            return data.Select(m => new GradesDto
            {
                Id = m.Id,
                Title = m.Title,
                CreateTime = m.CreateTime,
                UpdateTime = m.UpdateTime,
            }).ToList();
        }

        public async Task<GradesDto> GetGradesByIdAsync(Guid id)
        {
            var data = await _dal.GetById(id);
            return new GradesDto 
            {
                Id = data.Id,
                Title=data.Title,
            };
        }

        public Task<List<GradesDto>> GetGradesByNameAsync(string title)
        {
            throw new NotImplementedException();
        }

        public async Task<int> UpdateGradesAsync(Guid id, string title)
        {
            var data = await _dal.GetById(id);
            if(data == null)
                return 0;
            data.Title = title;
            data.UpdateTime = DateTime.Now;
            return await _dal.UpdateAsync(data);
        }

    }
}
